package com.treeliked.netdiskdemo.config;

import com.treeliked.netdiskdemo.model.Progress;
import org.apache.commons.fileupload.ProgressListener;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;

/**
 * 文件上传监听器
 *
 * @author lqs2
 * @date 2018/12/6, Thu
 */
@Component
public class FileUploadProgressListener implements ProgressListener {

    private HttpSession session;

    public void setSession(HttpSession session) {
        this.session = session;
        // 保存上传状态
        Progress status = new Progress();
        session.setAttribute("status", status);
    }

    @Override
    public void update(long l, long l1, int i) {
        Progress status = (Progress) session.getAttribute("status");
        status.setBytesRead(l);
        status.setContentLength(l1);
        status.setItems(i);
    }
}
